package com.baomidou.ant.service.impl;

import com.baomidou.ant.entity.Notice;
import com.baomidou.ant.mapper.NoticeMapper;
import com.baomidou.ant.service.INoticeService;
import com.baomidou.ant.vo.Response.ResponseNoticeVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2021-03-24
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper,Notice> implements INoticeService {
    @Value("${file.upload.path}")
    private String uploadPath;


    @Override
    public String upload(MultipartFile file) {
        // 获取上传文件名
        String filename = file.getOriginalFilename();
        // 定义上传文件保存路径
        String path = uploadPath+"noticeImages/";
        // 新建文件
        File filepath = new File(path, filename);
        // 判断路径是否存在，如果不存在就创建一个
        if (!filepath.getParentFile().exists()) {
            filepath.getParentFile().mkdirs();
        }
        try {
            file.transferTo(new File(path + File.separator + filename));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "noticeImage/"+ filename;
    }

    @Override
    public List<Notice> querryByStatus(String status) {
        QueryWrapper<Notice> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("status",status);
        return list(queryWrapper);
    }

    @Override
    public List<Notice> listOfUser() {
        QueryWrapper<Notice> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("status","0");
        List<Notice> noticeList=list(queryWrapper);
        return noticeList;
    }


}
